from google.appengine.api import memcache

from lib import cache
from lib import models


def Wipe(model):
  for obj in model.all():
    obj.delete()


def Populate():
  Wipe(models.Version)
  v1_4_3_BU = models.Version(version='1.4.3 BU').put()

  Wipe(models.Race)
  protoss = models.Race(name='Protoss').put()
  terran = models.Race(name='Terran').put()
  zerg = models.Race(name='Zerg').put()

  Wipe(models.Buildable)
  nexus = models.Buildable(
    race=protoss,
    version=v1_4_3_BU,
    name='Nexus',
    cost_minerals=400,
    build_seconds=100.0,
    provides_supply=10,
    armor=1.0,
    hit_points=100.00,
    shields=1000.0,
    sight=11.0,
    structure=True)
  nexus.put()

  probe = models.Buildable(
    race=protoss,
    version=v1_4_3_BU,
    name='Probe',
    cost_minerals=50,
    cost_supply=1,
    build_seconds=17.0,
    built_from=nexus,
    hit_points=20.0,
    shields=20.0,
    ground_attack=5.0,
    attack_cooldown_seconds=1.5,
    speed=2.8125,
    sight=8.0,
    light=True,
    mechanical=True,
    worker=True).put()

  nexus.requires.append(probe)
  nexus.put()

  pylon = models.Buildable(
    race=protoss,
    version=v1_4_3_BU,
    name='Pylon',
    cost_minerals=100,
    provides_supply=8,
    build_seconds=25.0,
    requires=[probe],
    armor=1.0,
    hit_points=200.0,
    shields=200.0,
    sight=9.0,
    structure=True).put()

  gateway = models.Buildable(
    race=protoss,
    version=v1_4_3_BU,
    name='Gateway',
    cost_minerals=150,
    build_seconds=65.0,
    requires=[probe, pylon],
    armor=1.0,
    hit_points=500.0,
    shields=500.0,
    sight=9.0,
    structure=True).put()

  zealot = models.Buildable(
    race=protoss,
    version=v1_4_3_BU,
    name='Zealot',
    cost_minerals=100,
    cost_supply=2,
    build_seconds=38.0,
    built_from=gateway,
    armor=1.0,
    hit_points=100.0,
    shields=50.0,
    ground_attack=16.0,
    air_attack=0.0,
    attack_cooldown_seconds=1.2,
    speed=2.25,
    range=0.0,
    sight=9.0,
    light=True,
    biological=True).put()

  Wipe(models.Map)
  map1 = models.Map(
    name='Test Map',
    starting_minerals=50,
    starting_gas=0).put()

  Wipe(models.StartingObject)
  models.StartingObject(
    map=map1,
    race=protoss,
    buildable=nexus).put()
  for _ in xrange(6):
    models.StartingObject(
      map=map1,
      race=protoss,
      buildable=probe).put()

  Wipe(models.Base)
  base1 = models.Base(
    map=map1).put()

  Wipe(models.WorkerGroup)
  models.WorkerGroup(
    base=base1,
    max_workers=16,
    resource_return_time=7.5,
    minerals_returned=5).put()

  models.WorkerGroup(
    base=base1,
    max_workers=8,
    resource_return_time=15.0,
    minerals_returned=5).put()

  models.WorkerGroup(
    base=base1,
    max_workers=6,
    resource_return_time=9.5,
    gas_returned=4).put()

  memcache.flush_all()
  cache.FlushLocalCache()
